package wjio;

import java.io.*;
import java.util.Scanner;

public class demo14_0527 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的源文件: ");
        //E:\vs背景\风景.jpg
        String srcPath = scanner.next();
        System.out.println("请输入要复制的目标文件: ");
        //E:\vs背景\风景1.jpg
        String destPath = scanner.next();

        File file = new File(srcPath);
        if (!file.isFile()) {
            System.out.println("源文件路径有误!");
            return;
        }

        File destFile = new File(destPath);
        if (!destFile.getParentFile().isDirectory()) {
            System.out.println("目标路径有误!");
            return;
        }

        try (InputStream inputStream = new FileInputStream(srcPath);
            OutputStream outputStream = new FileOutputStream(destFile)){
            while (true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if (n == -1) {
                    break;
                }
                outputStream.write(buffer,0,n);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
